DEFINT A-Z

DECLARE SUB Center (x, Text$)
DECLARE SUB CreateQuestion ()
DECLARE SUB DisplayBackground ()
DECLARE SUB DisplayReport ()
DECLARE SUB GetNameSubjectLevel ()
DECLARE SUB Math ()
DECLARE SUB Menu ()
DECLARE SUB SaveReport ()
DECLARE SUB SetVariables ()
OPTION BASE 1
COMMON SHARED Answer, Nmbr1, Nmbr2, Operator$
COMMON SHARED Level, Name$, Right, Subject, Turn, Wrong
ON ERROR GOTO ErrorTrap
DIM SHARED Subject$(10)
SCREEN 0: WIDTH 80, 25: COLOR 7, 0: CLS
SetVariables
CALL Menu

ErrorTrap:
COLOR 0, 7: LOCATE 5
SELECT CASE ERR
 CASE 7, 14
  Center 0, " Not enough memory. "
 CASE 53
  Center 0, " Cannot find Report.dat. "
 CASE 61
  Center 0, " The disk is full. "
 CASE 70
  Center 0, " Disk or file is  "
  Center 0, " write protected. "
 CASE 71
  Center 0, " No disk in diskdrive. "
 CASE ELSE
  Center 0, " Unexpected error. "
END SELECT
COLOR 7, 0: Center 0, " Escape = Quit   Enter = Try Again "
 DO
  Key$ = INPUT$(1)
   IF Key$ = CHR$(13) THEN RESUME
   IF Key$ = CHR$(27) THEN COLOR 7, 0: CLS : SYSTEM
 LOOP

SUB Center (x, Text$)
LOCATE , INT(40 - (LEN(Text$) / 2))
 IF x > 0 THEN LOCATE x
PRINT Text$
END SUB

SUB CreateQuestion
 IF Subject = 1 THEN
  Operator$ = "+"
  Nmbr1 = INT(RND * 10 * Level)
  Nmbr2 = INT(RND * 10 * Level)
  Answer = Nmbr1 + Nmbr2
 ELSEIF Subject = 2 THEN
  DO
   Operator$ = "-"
   Nmbr1 = INT(RND * 10 * Level)
   Nmbr2 = INT(RND * 10 * Level)
    IF Nmbr1 - Nmbr2 >= 0 THEN Answer = Nmbr1 - Nmbr2: EXIT DO
  LOOP
 ELSEIF Subject = 3 THEN
  Operator$ = "x"
   IF Level > 2 THEN
    Nmbr1 = INT(RND * 10 * (Level / 2))
    Nmbr2 = INT(RND * 10 * (Level / 2))
   ELSE
    Nmbr1 = INT(RND * 10 * Level)
    Nmbr2 = INT(RND * 10 * Level)
   END IF
  Answer = Nmbr1 * Nmbr2
 ELSEIF Subject = 4 THEN
  DO
   Operator$ = ":"
    IF Level > 2 THEN
     Nmbr1 = INT(RND * 10 * (Level / 2))
     Nmbr2 = INT(RND * 10 * (Level / 2))
    ELSE
     Nmbr1 = INT(RND * 10 * Level)
     Nmbr2 = INT(RND * 10 * Level)
    END IF
    IF NOT Nmbr2 = 0 THEN
     IF Nmbr1 MOD Nmbr2 = 0 THEN Answer = Nmbr1 / Nmbr2: EXIT DO
    END IF
  LOOP
 ELSEIF Subject = 5 THEN
  Nmbr1 = INT(RND * 10 * Level * 2) + 1
  Nmbr2 = INT(RND * 10 * Level * 2) + 1
  Answer = Nmbr1 * Nmbr2
 ELSEIF Subject = 6 THEN
  Nmbr1 = INT(RND * 10 * Level * 2) + 1
  Nmbr2 = INT(RND * 10 * Level * 2) + 1
  Answer = (Nmbr1 + Nmbr2) * 2
 END IF
END SUB

SUB DisplayBackground
LOCATE 1, 1
COLOR 7, 0: PRINT "Ú"; STRING$(35, "Ä"); "´";
COLOR 0, 7: PRINT " Math ";
COLOR 7, 0: PRINT "Ã"; STRING$(35, "Ä"); "¿"
 FOR Row = 1 TO 21
  PRINT "³"; SPC(78); "³"
 NEXT Row
PRINT "À"; STRING$(78, "Ä"); "Ù"
LOCATE 20, 5: PRINT "Math, by: Peter Swinkels, 1996"
END SUB

SUB DisplayReport
Row = 0
CLS
OPEN "Report.dat" FOR INPUT AS 1
 DO
  RowCnt = LOF(1) / 82
  SEEK #1, Row * 82 + 1
   FOR FileRow = 1 TO 21
     IF EOF(1) THEN EXIT FOR
    LINE INPUT #1, RowData$
    LOCATE FileRow + 2, 1: PRINT RowData$
   NEXT FileRow
  LOCATE 1, 1: COLOR 0, 7: PRINT " Right:       Wrong:         Subject:                     Level:    Name:      "
  COLOR 7, 0
  LOCATE 2, 2: PRINT "Del = Remove report."; SPACE$(54)
   DO
    Key$ = INKEY$
   LOOP WHILE Key$ = ""
  LOCATE 3, 1: PRINT SPACE$(1680)
  SELECT CASE Key$
   CASE CHR$(27)
    CLOSE 1: EXIT SUB
   CASE CHR$(0) + "H"
    IF Row > 0 THEN Row = Row - 1
   CASE CHR$(0) + "P"
    IF Row < RowCnt THEN Row = Row + 1
   CASE CHR$(0) + "S"
    LOCATE 2, 2: PRINT "Remove the report (y/n)?"; SPACE$(47)
     DO
      Key$ = UCASE$(INPUT$(1))
       IF Key$ = "Y" THEN
        CLOSE 1
        OPEN "Report.dat" FOR OUTPUT AS 1: CLOSE 1
        Row = 0
        CLS
        OPEN "Report.dat" FOR INPUT AS 1
        EXIT DO
       ELSEIF Key$ = "N" THEN
        EXIT DO
       END IF
     LOOP
   END SELECT
 LOOP
END SUB

SUB GetNameSubjectLevel
CLS
 DO
  l = LEN(Name$)
  COLOR 7: LOCATE 2, 3: PRINT "Name: "; Name$;
  COLOR 23: PRINT "_ ";
   DO
    Key$ = INKEY$
   LOOP WHILE Key$ = ""
  SELECT CASE Key$
   CASE CHR$(8)
    IF l > 0 THEN Name$ = LEFT$(Name$, l - 1)
   CASE CHR$(13)
    LOCATE 2, l + 9: PRINT " "
    EXIT DO
   CASE ELSE
    IF l < 10 THEN
     KeyAsc = ASC(Key$)
      IF KeyAsc > 31 AND KeyAsc < 127 THEN Name$ = Name$ + Key$
    END IF
   END SELECT
 LOOP

COLOR 7

LOCATE 4, 3: PRINT "Choice (1-6):"
 FOR Subject = 1 TO 6
  LOCATE , 4: PRINT Subject; "= "; Subject$(Subject)
 NEXT Subject
Subject = VAL(INPUT$(1))
 IF Subject = 0 OR Subject > 6 THEN EXIT SUB

PRINT
PRINT "    Level (1-9):"
Level = VAL(INPUT$(1))
 IF Level = 0 THEN EXIT SUB
END SUB

SUB Math
GetNameSubjectLevel
 IF Subject = 0 OR Subject > 6 THEN EXIT SUB
 IF Level = 0 THEN EXIT SUB

CLS
l = 39 - LEN(Subject$(Subject)) \ 2
LOCATE 1, 1: PRINT STRING$(l, "Ä") + " " + Subject$(Subject) + " " + STRING$(l, "Ä")
CreateQuestion
 DO
  l = LEN(Answer$)
  LOCATE 2, 1
  PRINT USING "  Right: ###  Wrong: ###  Turn: ###"; Right; Wrong; Turn
  PRINT STRING$(80, "Ä")
  LOCATE 5, 5
   IF Subject < 5 THEN
    PRINT Nmbr1; Operator$; Nmbr2; "= "; Answer$;
   ELSEIF Subject = 5 THEN
    PRINT "There is an area of"; Nmbr1; "by"; Nmbr2; "metres, what is the area? "; Answer$;
   ELSEIF Subject = 6 THEN
    PRINT "There is a surface of"; Nmbr1; "by"; Nmbr2; "metres, what is the outline? "; Answer$;
   END IF
  PRINT SPC(15);
   DO
    Key$ = INKEY$
   LOOP WHILE Key$ = ""
  KeyVal = VAL(Key$)
  SELECT CASE Key$
   CASE CHR$(8)
    IF LEN(Answer$) > 0 THEN Answer$ = LEFT$(Answer$, l - 1)
   CASE CHR$(13)
     IF VAL(Answer$) = Answer THEN
      Right = Right + 1
     ELSE
      SOUND 37, 5: Wrong = Wrong + 1
     END IF
    Answer$ = ""
     IF Turn = 25 THEN
      SaveReport
      EXIT SUB
     ELSE
      Turn = Turn + 1: CreateQuestion
     END IF
   CASE CHR$(27)
    CLS : EXIT SUB
   CASE ELSE
    IF KeyVal >= 0 AND KeyVal < 10 THEN
     IF l < 11 THEN Answer$ = Answer$ + Key$
    END IF
  END SELECT
 LOOP
END SUB

SUB Menu
DIM Item$(3)
CursorX = 1
Item$(1) = " Math     "
Item$(2) = " Report   "
Item$(3) = " Quit     "
 DO
  DisplayBackground
   FOR Item = 1 TO 3
     IF Item = CursorX THEN COLOR 0, 7 ELSE COLOR 7, 0
    LOCATE (Item * 2) + 3, 10: PRINT Item$(Item)
   NEXT Item
   DO
    Key$ = INKEY$
   LOOP WHILE Key$ = ""
  SELECT CASE Key$
   CASE CHR$(0) + "H"
    IF CursorX = 1 THEN CursorX = 3 ELSE CursorX = CursorX - 1
   CASE CHR$(0) + "P"
    IF CursorX = 3 THEN CursorX = 1 ELSE CursorX = CursorX + 1
   CASE CHR$(13)
     IF CursorX = 1 THEN CALL Math
     IF CursorX = 2 THEN DisplayReport
     IF CursorX = 3 THEN COLOR 7, 0: CLS : SYSTEM
    SetVariables
   END SELECT
  LOOP
 END SUB

SUB SaveReport
OPEN "Report.dat" FOR APPEND AS 1
 PRINT #1, USING "###"; Right;
 PRINT #1, ,
 PRINT #1, USING "###"; Wrong;
 PRINT #1, , Subject$(Subject); SPACE$(18 - LEN(Subject$(Subject))), Level, Name$; SPACE$(10 - LEN(Name$))
CLOSE 1
END SUB

DEFSNG A-Z
SUB SetVariables
Subject$(1) = "Adding"
Subject$(2) = "Subtracting"
Subject$(3) = "Multiplying"
Subject$(4) = "Dividing"
Subject$(5) = "Area"
Subject$(6) = "Outline"
Level = 1
Name$ = ""
Right = 0
Turn = 0
Wrong = 0
END SUB

